<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/common.jsp"%>  
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
    content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <title></title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <link rel="stylesheet" type="text/css"
            href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
        <link rel="stylesheet" type="text/css"
            href="<c:url value="/static/jquery-easyui-mobile/themes/mobile.css"/>"> 
        <link rel="stylesheet" type="text/css" href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
        <script type="text/javascript"
            src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
        <script type="text/javascript" src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>   
        <script type="text/javascript"
            src="<c:url value="/static/js/common.js"/>"></script>       
        <script>
        $(window).bind( 'orientationchange', function(e){
            window.location.reload();
        });  
            function initList(listDisplayEId) {
                $('#' + listDisplayEId).datagrid(
                        {
                            nowrap : false,
                            striped : true,
                            collapsible : true,
                            remoteSort : true,
                            notSelectByRow : false,
                            /* frozenColumns : [ [ {
                                field : 'ids',
                                checkbox : true
                            } ] ], */
                            
                            toolbar : [
                                    
                                    ],
                            onLoadError:function(){
                                $.messager.alert('错误','获取数据失败，请与管理员联系！','error');
                            },
                            onLoadSuccess:function(){ 
                                $(".datagrid-row").mouseover(function(e){  
                                    if(checkHover(e,this)){
                                        var foodId = $(this).find('td').eq(1).text();
                                        var index = $('#listDisplay').datagrid('getRowIndex', foodId);
                                        $('#listDisplay').datagrid('beginEdit',index);
                                    }
                                }); 
                            } 
                        });
                
            };
            
            function initList2(listDisplayEId) {
                $('#' + listDisplayEId).datagrid(
                        {
                            nowrap : false,
                            striped : true,
                            collapsible : true,
                            remoteSort : true,
                            notSelectByRow : false,
                            onLoadSuccess:function(){ 
                                $(".datagrid-row").mouseover(function(e){  
                                    if(checkHover(e,this)){
                                        var foodId = $(this).find('td').eq(0).text();
                                        var index = $('#listDisplay2').datagrid('getRowIndex', foodId);
                                        $('#listDisplay2').datagrid('beginEdit',index);
                                    }
                                }); 
                                
                                $(".datagrid-row").mouseout(function(e){  
                                    if(checkHover(e,this)){
                                        var foodId = $(this).find('td').eq(0).text();
                                        var index = $('#listDisplay2').datagrid('getRowIndex', foodId);
                                        $('#listDisplay2').datagrid('endEdit',index);
                                    }
                                });
                            } 
                        });
                       
                
            };
            $(document).ready(function() {
                initList2('listDisplay2');
                initList('listDisplay');
            });
            
            function submitzzy(){
                var rows = $('#listDisplay2').datagrid('getRows'); 
                for(var i=0; i<rows.length; i++){
                    var rowIndex=$('#listDisplay2').datagrid('getRowIndex',rows[i]['sid']);
                    $('#listDisplay2').datagrid('endEdit', rowIndex);
                }
                var row = $('#listDisplay2').datagrid('getRows');  
                if(row.length == 0){
                    $.messager.alert('提示','请选择一条记录操作！','info');
                    return;
                }else{
                    var userList = new Array(); 
                    var jsonFater;
                    for (var i = 0 ; i < row.length;i++){
                        
                        if ( row[i]['materNum'] == "" || !isNumberAndNoZero(row[i]['materNum'])  ){
                            alert("材料"+row[i]['materName']+"，（单位数量 ）请填写数字！");
                            return false;
                        }
                        var  jsonSun = "{materId:'"+row[i]['materId']+"',supId:'"+row[i]['supId']+"',materNum:'"+row[i]['materNum']+"',leaveMessage:'"+row[i]['leaveMessage']+"',materLevel:'"+row[i]['materLevel']+"'}";
                        userList.push(jsonSun);
                    }
                    jsonFater = JSON.stringify(userList);
                    var str = "json="+jsonFater;    
                    document.getElementsByName("materdategridList")[0].value = jsonFater;
                    $.messager.confirm('提示', '是否提交记录？', function(r){
                        //方法函数 进入后台
                        if(r){
                            addMater();
                        }
                    });
                }
                
            }
            
            function addMater(){
                $('#AddMaterForm').form('submit',
                {
                    url:'<c:url value='/order/commitMaterOrderForUpdate.do'/>', //查询提交路径
                    onSubmit:function()//提交按钮相应函数
                    {
                        return $('#AddMaterForm').form('validate');
                    },
                    success:function(data){
                        data = eval( "("+data+")" );
                        if(data.success){
                            $.messager.alert("提示", "提交成功！", 'info');
                            $('#listDisplay').datagrid('reload');
                            $('#listDisplay').datagrid('clearSelections');
                            $('#listDisplay2').datagrid('reload');
                            $('#listDisplay2').datagrid('clearSelections');
                        }else{
                            var message = "";
                            if(data.msg == "msg002"){
                                message = "所选择日期的材料订单已经下单完成，如需重新选择请删除已有订单。";
                            }else if(data.msg == "msg001"){
                                message = "选择材料的时间已过，请联系采购部。";
                            }
                            $.messager.alert("提示", message, 'warning');
                        }
                    },
                    error:function(){
                        $.messager.alert("错误", "系统错误，请联系管理员！", 'error');
                        return;
                    }
                });
                
            }
            //下拉框
            $(function() {        
                $('#materType').combobox({   
                    url:"<c:url value='/mater/pulldownMaterType.do?space=1'/>",   
                    editable:true, //不可编辑状态  
                    cache: false,  
                    panelHeight: '150',  
                    valueField:'materType',     
                    textField:'materTypename',                      
                    }); 
            });
            function searchInfo(){
                if($('#materType').combobox('getValue') ==''){
                    $('#listDisplay').datagrid('load', {
                        materType : 0,
                        materName : $('#materName').val(),
                        supName : $('#supName').val()
                    }); 
                }else{
                    $('#listDisplay').datagrid('load', {
                        materType : $('#materType').combobox('getValue'),
                        materName : $('#materName').val(),
                        supName : $('#supName').val()
                    });  
                }
            }
            
            function  goDetailedContent(val,rowData,rowIndex){
                return '<a href="#" onclick="constructionManager(\'' + rowIndex+ '\')">'+val+'</a>  '
            }
            
            function constructionManager(rowIndex) {
                var rows = $('#listDisplay').datagrid('getRows');   
                window.open("<c:url value='/jsp/openIndexSun.jsp?url=/mater/preLookMaterValue.do'/>"+"?materId="+rows[rowIndex]['materId']+"&materLevel="+rows[rowIndex]['materLevel'] +"&supId="+rows[rowIndex]['supId'] ,"sun","width=1200px,height=600px,status=no,scrollbars=yes");
            }
            function formatOptionField(val,rowData,rowIndex){
                var a= rowData['sid']+'';
                return "<a href='###' onclick='editrow("+a+")'><img src='../images/cancel.png'/></a>";
            }
            function formatShoppingCart(val,rowData,rowIndex){
                return "<a href='###' onclick='shoppingCart("+rowIndex+")'><img src='../images/gouwuche.png'/></a>";
            }
            function editrow(sid){
                $.ajax({
                    async : false,
                    url : "<c:url value='/mater/deleteShoppingCartOne.do'/>" +"?sid="+sid,
                    type : "post",
                    success : function(data) {
                        if(typeof(data)=='string'){
                            dataJson =JSON.parse(data);
                        }else{
                            dataJson = data;
                        }
                        if(dataJson.success){
                            $.messager.alert('提示','删除成功','info');
                            $('#listDisplay2').datagrid('load');
                        }else{
                            $.messager.alert('提示','删除失败，请刷新页面重新删除！','info');
                        }
                    }
                });
            }
            function shoppingCart(rowIndex){
            	var rowData ;
                var a ;
                var dateone;
                $('#listDisplay').datagrid('endEdit', rowIndex);
                var rowDateAll = $('#listDisplay').datagrid('getRows');
                for(var i=0; i<rowDateAll.length; i++){
                    if(rowIndex ==$('#listDisplay').datagrid('getRowIndex',rowDateAll[i]['mid'])) {
                        $('#listDisplay').datagrid('endEdit', rowIndex);
                        rowData = rowDateAll[i];
                        if ( !isNumberAndNoZero(rowData['materNum'])  ){
                            $.messager.alert('提示',"材料"+rowData['materName']+"，数量请填写数字！",'info');
                            return false;
                        }
                        if(strlen(rowData['leaveMessage'])>50){
                            $.messager.alert('提示',"材料"+rowData['materName']+"，留言请小于50字！",'info');
                            return false;
                        }
                        var leaveMessage =rowData['leaveMessage'];
                        if(leaveMessage==undefined){
                            var leaveMessage = "";
                            return false;
                        }
                        dateone = '{"materId":"'+ rowData['materId']+'","materNum":"'+rowData['materNum'] +'","materLevel":"'+rowData['materLevel']+'","materValue":"' + rowData['materValue']+'","materProportion":"'+ rowData['materProportion'] +'","materValueUndressed":"'+ rowData['materValueUndressed']+'","supId":"'+ rowData['supId']+'","custId":"${sessionScope.user.userId }","leaveMessage":"'+leaveMessage+'"}';
                        break;
                    }
                }
                dataOneJson =JSON.parse(dateone);
                $.ajax({
                    async : false,
                    url : "<c:url value='/mater/addShoppingCart.do'/>" ,
                    type : "post",
                    dataType: "json",
                    data: $.param(dataOneJson),
                    success : function(data) {
                        if(typeof(data)=='string'){
                            dataJson =JSON.parse(data);
                        }else{
                            dataJson = data;
                        }
                        if(dataJson.success){
                            $.messager.alert('提示','成功放入购物车。','info');
                            $('#listDisplay2').datagrid('load');
                        }else{
                            $.messager.alert('提示','购物车已有此种商品，请在购物车中修改数量！','info');
                        }
                    }
                });
            }
        </script>
            </head>
            <body><!--
            <div class="m-toolbar">
                <div class="m-title">材 料 选 择</div>
                
                <div class="m-left">
                    <a href="javascript:void(0)" class="easyui-linkbutton m-back" plain="true" outline="true">Back</a>
                </div>
                
            </div>-->
            <header>
            <div class="m-toolbar">
                <span class="m-title">材料选择界面</span>
            </div>
            </header>
            <form id="AddMaterForm" name="AddMaterForm" method="post">
            <table width="100%">
            <tr><td>
            <div style="height: auto; width:100%;background: #fafafa; padding: 2px;" class="easyui-panel" title="&nbsp&nbsp&nbsp&nbsp&nbsp必录信息"  collapsible="true" minimizable="false" maximizable="false" closable="false">
               <table cellpadding="5">
                  <tr>
                    <td style="width:80px;">材料使用日期</td>
                    <td>
                        <input class="easyui-validatebox" style="width:180px; height: 30px;" value="${selectMaterVo.ordDate }" name="orderDate" id="orderDate" editable="false" readOnly="true"/> 
                    </td>
                  </tr>
                  <tr>
                    <td style="width:80px;">订单号</td>
                    <td>
                        <input class="easyui-validatebox" style="width:180px; height: 30px;" value="${selectMaterVo.ordId }" name="ordId" id="ordId" editable="false" readOnly="true"/> 
                    </td>
                  </tr>
                  <tr>
                        <input class="easyui-validatebox" type="hidden" name="materdategridList"  />
               </table>
             </div>
           </td></tr>
           <tr><td>
            <div style="height: auto; width:100%;background: #fafafa; padding: 5px;" class="easyui-panel" title="查询" iconCls="icon-search" collapsible="true" minimizable="false" maximizable="false" closable="false">
                    <form id="searchForm" name="searchForm" method="post"
                        action="userlist.txt"
                        onsubmit="return searchDGData(this.id,'listDisplay');">
                        <div>
                        <table cellpadding="5">
                                <tr>
                                    <td colspan="2" ><span style="color:red;">注意！材料名支持拼音和首字母检索</span></td>
                                </tr>
                                <tr>
                                    <td style="width:80px;">材料名：</td>
                                    <td><input style="width: 180px; height: 30px;" class="easyui-validatebox" id="materName" editable="true" name="materName"></input></td>
                                </tr>
                                <tr>
                                    <td style="width:80px;">类别：</td>
                                    <td><input style="width: 180px; height: 30px;" class="easyui-combobox" id="materType" editable="false" name="materType"></input></td>
                                </tr>
                                <tr>
                                    <td style="width:80px;">供货商名：</td>
                                    <td><input style="width: 180px; height: 30px;" class="easyui-validatebox" id="supName" editable="true" name="supName"></input></td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                    <div style="text-align: center; margin-top: 15px">
                                        <a href="#" onclick="searchInfo()" class="easyui-linkbutton" style="width: 50%; height: 40px"><span style="font-size: 16px">查询</span></a>
                                    </div>
                                    </td>
                                </tr>
                                
                        </table>                     
                        </div>
                    </form>
                </div>
            </td></tr>
           </table>
        <table width="100%">
        <tr style="height: 12px;"></tr>
        <tr>
        <td>
                <table id="listDisplay"  width="100%" title="选取材料列表" 
            iconCls="icon-list" singleSelect="false" idField="mid"
            url="<c:url value="/mater/findSelectMater.do"/>" loadMsg="请稍候......"
            pagination="true" pageSize="10" 
            pageList="[10,20,30,40,50,100]" sortName="mid"
            sortOrder="asc">
            <thead>
                <tr>
                    <th field="button" width="120" align="center" formatter="formatShoppingCart">购物车</th>
                    <th field="mid" width="80" sortable="true" hidden="true">ID</th>
                    <th field="materNum" width="50" sortable="true" editor="text" align="center" >数量</th>
                    <th field="materId" width="40" sortable="true" hidden="true" frozen="true">材料ID</th>    
                    <th field="materName" width="80"  sortable="true">材料名</th> 
                    <th field="materType" width="55" formatter="formatFoodType1" sortable="true" >种类</th>   
                    <th field="unit" width="60"  sortable="true" >单位
                    </th>
                    <th field="brands" width="60" sortable="true" >品牌
                    </th>
                    <th field="specifications" width="60" sortable="true" >规格
                    </th>
                    <th field="materLevel" width="40" formatter="formatFoodType" sortable="true" hidden="true" >品级</th>   
                    <th field="supId" width="80" sortable="true" hidden="true">供货商ID</th>
                    <th field="supName" width="60" sortable="true" >供货商</th>
                    <th field="materValue" width="70"  sortable="true" align="right" >净料价格 </th>   
                    <th field="materValueUndressed" width="60"  sortable="true" align="right">毛料价格</th> 
                    <th field="materProportion" width="100"  sortable="true" align="right" hidden="true">毛料率（%）</th>
                    <th field="leaveMessage" width="100"  editor="text"  sortable="true" align="right">留言
                    </th>
                </tr>
            </thead>
            <thead data-options="frozen:true">
                <tr>
                </tr>
            </thead>
            </table>
            </td>
            </tr>
            <tr style="height: 12px;"></tr>
        <tr>
        <td>
                <table id="listDisplay2" title="购物车列表" width="100%"
                url="<c:url value="/mater/findShoppingCartMater.do"/>"
            iconCls="icon-list" singleSelect="false" idField="mid"
             loadMsg="请稍候......"
            pagination="false" sortName="mid"
            rownumbers="true" sortOrder="asc">
            <thead>
                <tr>
                    <th field="button" width="40" align="center" formatter="formatOptionField">删除</th>
                    <th field="materNum" width="50" sortable="true" editor="text" align="center" >数量</th>
                    <th field="materId" width="50" sortable="true" hidden="true" frozen="true">材料ID</th>    
                    <th field="materName" width="140"  sortable="true" formatter="goDetailedContent" >材料名</th> 
                    <th field="materType" width="55" formatter="formatFoodType1" sortable="true" >种类</th>   
                    <th field="unit" width="60"  sortable="true" >单位
                    </th>
                    <th field="brands" width="60" sortable="true" >品牌
                    </th>
                    <th field="specifications" width="60" sortable="true" >规格
                    </th>
                    <th field="materLevel" width="40" formatter="formatFoodType" sortable="true" hidden="true">品级</th>   
                    <th field="supId" width="80" sortable="true" hidden="true">供货商ID</th>
                    <th field="supName" width="60" sortable="true" >供货商</th>
                    <th field="materValue" width="60"  sortable="true" align="right" >净料价格 </th>   
                    <th field="materValueUndressed" width="60"  sortable="true" align="right">毛料价格</th> 
                    <th field="materProportion" width="100"  sortable="true" align="right" hidden="true">毛料率（%）</th>
                    <th field="leaveMessage" width="100"  editor="text"  sortable="true" align="right">留言
                    </th>
                </tr>
            </thead>
            <!-- 
            <thead data-options="frozen:true">
                <tr>
                    
                </tr>
            </thead>
             -->
            </table>
            </td>
            </tr>
        </table>
        </form>
        <div style="text-align: center; margin-top: 15px">
            <a href="###" onclick="submitzzy();" class="easyui-linkbutton" style="width: 50%; height: 40px"><span style="font-size: 16px">提交</span></a>
        </div>
        <br/>
        <br/>
        <br/>
        
    </body>
<style>
body{margin:0;padding:0;}
.m-toolbar{
    position: relative;
    text-align: center;
    min-height: 32px;
    background-color:#e0edfe ;
}
.datagrid-row {
    height: 32px;
}

</style>
</html>